//@author A0096759E

package testcases;

import static org.junit.Assert.assertEquals;
import logic.ScheduleSearcher;
import logic.ScheduleSorter;

import org.junit.Test;

import storage.ScheduleEvent;
import storage.Storage;

public class ScheduleSearcherTestSearch {
	
	private static final String SUCCESS_ALL_EVENTS_CLEARED = "All events have been cleared from your Schedule!";
	private static final String SUCCESS_ADDED_EVENT = "The event \"%s\" has been added successfully to your Schedule!";
	private static final String SEARCH_MESSAGE = "Entries with search term \"%s\" in your Schedule:";
	private static final String SEARCH_MESSAGE_TYPE = "Entries with the type \"%s\" in your Schedule:";
	private static final String SEARCH_MESSAGE_DATE = "Entries on \"%s\" in your Schedule:";
	private static final String SEARCH_MESSAGE_DATE_RANGE = "Entries from \"%s\" to \"%s\" in your Schedule:";
	
	private static final String ERROR_MSG_INVALID_SEARCH_COMMAND = "ERROR! The search command was not recognized. Please try again!";
	private static final String ERROR_MSG_SEARCH_RESULTS_NOT_FOUND = "No search results found. Please try again!";
	
	Storage storageTest = new Storage("JUnit ScheduleSeacher Test.txt");
	ScheduleSorter sorter = new ScheduleSorter(storageTest);
	ScheduleSearcher searcher = new ScheduleSearcher(storageTest, sorter);
	
	@Test
	public void test() {
		assertEquals(SUCCESS_ALL_EVENTS_CLEARED,storageTest.clearAllEvents());
		StringBuilder correctSequence = new StringBuilder();
		
		ScheduleEvent eventToAdd1 = new ScheduleEvent(0, "Apple Juice", "20121991");
		ScheduleEvent eventToAdd2 = new ScheduleEvent(1, "Banana Milkshake", "21121991", "1200");
		ScheduleEvent eventToAdd3 = new ScheduleEvent(2, "Carrots Are Nice", "22121991", "1200", "1230");
		ScheduleEvent eventToAdd4 = new ScheduleEvent(0, "Donkeys are short", "23121991");
		
		correctSequence.append("\n\n1. " + eventToAdd1.toString());
		correctSequence.append("\n\n4. " + eventToAdd4.toString());
		
		assertEquals(String.format(SUCCESS_ADDED_EVENT, "Donkeys are short" ),storageTest.addEvent(eventToAdd4));
		assertEquals(String.format(SUCCESS_ADDED_EVENT, "Banana Milkshake" ),storageTest.addEvent(eventToAdd2));
		assertEquals(String.format(SUCCESS_ADDED_EVENT, "Carrots Are Nice" ),storageTest.addEvent(eventToAdd3));
		assertEquals(String.format(SUCCESS_ADDED_EVENT, "Apple Juice" ),storageTest.addEvent(eventToAdd1));
		
		assertEquals(String.format(SEARCH_MESSAGE_TYPE,"Floating") + correctSequence.toString(), searcher.searchEvents("float"));
		assertEquals(ERROR_MSG_SEARCH_RESULTS_NOT_FOUND , searcher.searchEvents("-sc ABNN"));
		assertEquals(ERROR_MSG_INVALID_SEARCH_COMMAND , searcher.searchEvents("-sc"));
		storageTest.saveEvents();
		
		//Search By Keyword
		assertEquals(SUCCESS_ALL_EVENTS_CLEARED,storageTest.clearAllEvents());
		correctSequence = new StringBuilder();
		
		eventToAdd1 = new ScheduleEvent(0, "Apple Juice", "20121991");
		eventToAdd2 = new ScheduleEvent(1, "Banana Milkshake", "21121991", "1200");
		eventToAdd3 = new ScheduleEvent(2, "Carrots Are Nice", "22121991", "1200", "1230");
		eventToAdd4 = new ScheduleEvent(0, "Donkeys are short", "23121991");
		
		correctSequence.append("\n\n4. " + eventToAdd4.toString());
		
		assertEquals(String.format(SUCCESS_ADDED_EVENT, "Donkeys are short" ),storageTest.addEvent(eventToAdd4));
		assertEquals(String.format(SUCCESS_ADDED_EVENT, "Banana Milkshake" ),storageTest.addEvent(eventToAdd2));
		assertEquals(String.format(SUCCESS_ADDED_EVENT, "Carrots Are Nice" ),storageTest.addEvent(eventToAdd3));
		assertEquals(String.format(SUCCESS_ADDED_EVENT, "Apple Juice" ),storageTest.addEvent(eventToAdd1));
		
		assertEquals(String.format(SEARCH_MESSAGE,"donkeys") + correctSequence.toString(), searcher.searchEvents("donkeys"));
		assertEquals(String.format(SEARCH_MESSAGE,"Donkeys") + correctSequence.toString(), searcher.searchEvents("-acsa Donkeys"));
		assertEquals(ERROR_MSG_SEARCH_RESULTS_NOT_FOUND , searcher.searchEvents("-sc ABNN"));
		assertEquals(ERROR_MSG_INVALID_SEARCH_COMMAND , searcher.searchEvents("-sc"));
		storageTest.saveEvents();
		
		//Search By Date
		assertEquals(SUCCESS_ALL_EVENTS_CLEARED,storageTest.clearAllEvents());
		correctSequence = new StringBuilder();
		
		eventToAdd1 = new ScheduleEvent(0, "Apple Juice", "20121991");
		eventToAdd2 = new ScheduleEvent(1, "Banana Milkshake", "21121991", "1200");
		eventToAdd3 = new ScheduleEvent(2, "Carrots Are Nice", "22121991", "1200", "1230");
		eventToAdd4 = new ScheduleEvent(0, "Donkeys are short", "23121991");

		correctSequence.append("\n\n3. " + eventToAdd3.toString());
		correctSequence.append("\n\n4. " + eventToAdd4.toString());
		
		assertEquals(String.format(SUCCESS_ADDED_EVENT, "Donkeys are short" ),storageTest.addEvent(eventToAdd4));
		assertEquals(String.format(SUCCESS_ADDED_EVENT, "Banana Milkshake" ),storageTest.addEvent(eventToAdd2));
		assertEquals(String.format(SUCCESS_ADDED_EVENT, "Carrots Are Nice" ),storageTest.addEvent(eventToAdd3));
		assertEquals(String.format(SUCCESS_ADDED_EVENT, "Apple Juice" ),storageTest.addEvent(eventToAdd1));
		
		assertEquals(String.format(SEARCH_MESSAGE_DATE_RANGE,"22121991", "24121991") + correctSequence.toString(), searcher.searchEvents("22121991 24121991"));
		assertEquals(ERROR_MSG_SEARCH_RESULTS_NOT_FOUND , searcher.searchEvents("-sc 24121991 26121991"));
		assertEquals(ERROR_MSG_INVALID_SEARCH_COMMAND , searcher.searchEvents("-sc"));
		storageTest.saveEvents();
		
		//Search By Date Range
		assertEquals(SUCCESS_ALL_EVENTS_CLEARED,storageTest.clearAllEvents());
		correctSequence = new StringBuilder();
		
		eventToAdd1 = new ScheduleEvent(0, "Apple Juice", "20121991");
		eventToAdd2 = new ScheduleEvent(1, "Banana Milkshake", "21121991", "1200");
		eventToAdd3 = new ScheduleEvent(2, "Carrots Are Nice", "22121991", "1200", "1230");
		eventToAdd4 = new ScheduleEvent(0, "Donkeys are short", "23121991");
		
		correctSequence.append("\n\n3. " + eventToAdd3.toString());
		
		assertEquals(String.format(SUCCESS_ADDED_EVENT, "Donkeys are short" ),storageTest.addEvent(eventToAdd4));
		assertEquals(String.format(SUCCESS_ADDED_EVENT, "Banana Milkshake" ),storageTest.addEvent(eventToAdd2));
		assertEquals(String.format(SUCCESS_ADDED_EVENT, "Carrots Are Nice" ),storageTest.addEvent(eventToAdd3));
		assertEquals(String.format(SUCCESS_ADDED_EVENT, "Apple Juice" ),storageTest.addEvent(eventToAdd1));
		
		assertEquals(String.format(SEARCH_MESSAGE_DATE,"22121991") + correctSequence.toString(), searcher.searchEvents("22121991"));
		assertEquals(ERROR_MSG_SEARCH_RESULTS_NOT_FOUND , searcher.searchEvents("-sc 24121991"));
		assertEquals(ERROR_MSG_INVALID_SEARCH_COMMAND , searcher.searchEvents("-sc"));
		storageTest.saveEvents();
	}

}
